﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using MMOShared;

namespace MMOResCreator
{
    public partial class FinalizeVersionForm : Form
    {
        private MySqlConnection connection;
        private MySqlCommand command;
        private MasterVersionList versions;
        private List<UInt16> serverVersions;
 
        public FinalizeVersionForm(MasterVersionList versions)
        {
            this.serverVersions = new List<ushort>();
            this.versions = versions;
            InitializeComponent();
        }

        private void closeB_Click(object sender, EventArgs e)
        {
            if (connection.State == ConnectionState.Open)
            {
                connection.Close();
            }
            this.Close();
        }

        private void connectB_Click(object sender, EventArgs e)
        {
            try
            {
                string connectionString = "SERVER=" + serverTB.Text +
                        ";DATABASE=" + databaseTB.Text +
                        ";UID=" + userTB.Text +
                        ";PASSWORD=" + passwordTB.Text + ";";
                connection = new MySqlConnection(connectionString);
                command = connection.CreateCommand();
                connection.Open();

                if (connection.State == ConnectionState.Connecting || connection.State == ConnectionState.Open)
                {
                    uploadB.Enabled = true;
                    populateVersionsLV();
                }
            }
            catch (Exception er)
            {
                MessageBox.Show("Error: " + er.Message);
            }
        }

        private void populateVersionsLV()
        {
            UInt16[] localVersions = versions.GetLoadedVersionNumbers();
            versionsLV.Clear();

            command.CommandText = "SELECT * FROM `" + databaseTB.Text + "`.`updates`";
            MySqlDataReader dr = command.ExecuteReader();

            while (dr.Read())
            {
                serverVersions.Add(dr.GetUInt16(1));
            }
            dr.Close();

            foreach (UInt16 v in localVersions)
            {
                int ii = 0;
                if (serverVersions.Contains(v))
                {
                    ii = 1;
                }
                versionsLV.Items.Add(v.ToString(), ii);
            }
        }

        private void uploadB_Click(object sender, EventArgs e)
        {
            //try
            //{
            if (versionsLV.SelectedIndices.Count == 1)
            {
                int selI = versionsLV.SelectedIndices[0];
                ++selI;

                if ((!serverVersions.Contains((ushort)(selI)) && serverVersions.Contains((ushort)(selI - 1))) || selI == 1)
                {
                    ResourceManager r = versions.GetVersion((ushort)selI);
                    if (r != null)
                    {
                        ResourceManager oR = versions.GetVersion((ushort)(selI - 1));
                        FileUpdateStatistics stats = FileUpdatePiece.CreateUpdateDiffSQL(connection, oR, r);
                        UpdateStats(stats);
                        //update versions list view
                        populateVersionsLV();
                    }
                }
            }
            else
            {
                MessageBox.Show("You must first select a version that hasn't already been uploaded from the list.");
            }
            //catch (Exception er)
            //{
            //    MessageBox.Show("Error: " + er.Message);
            //}
        }

        private void UpdateStats(FileUpdateStatistics stats)
        {
            numOfFilesL.Text = "Number of Files: " + stats.NumberOfFiles.ToString();
            newFilesL.Text = "New Files: " + stats.NewFiles.ToString();
            updatedL.Text = "Updated: " + stats.FilesUpdated.ToString();
            numOfPiecesL.Text = "Number of Pieces: " + stats.NumberOfPieces.ToString();
            sizeL.Text = "Total Size: " + (stats.TotalSize / 1000).ToString() + " kb";
            deletionsL.Text = "Deletions: " + stats.Deletions.ToString();
        }
    }
}
